<?php
class Cms_View_Helper_ShowiQ
{
    public $view,$voteArray,$totalVotes;
    public function showiQ( $SectorId = 0, $VcpId = 0, $VcppId = 0, $votes, $type ) 
    {
        switch($type) {
        
        	case 'Article': 
            				if( !is_array($this->voteArray) ) {
								$query=new Doctrine_Query();
								$query->Select('DISTINCT iq_votes as count ')
									  ->from('Article')
									  ->where('iq_votes != 0');
								
								if($SectorId != 0)
									$query->where('sector_id='.$SectorId);
								
								if($VcpId != 0)
									$query->where('vcp_id='.$VcpId);
								
								if($VcppId != 0)
									$query->where('vcpp_id='.$VcppId);
									
								$query->orderBy('iq_votes ');
								//echo $query->getSqlQuery();
								//echo 'sect'.$SectorId.'vcp'.$VcpId .'vcpp'.$VcppId; 
								$array = $query->execute(array(), Doctrine::HYDRATE_ARRAY);
								foreach( $array as $value) {
									$this->voteArray[] = $value['count'];
								}
								 //print_r($this->voteArray);
								$this->totalVotes = count( $this->voteArray);
								//echo $this->totalVotes. 'total';
                            }
							//echo $votes.'postion';
						  	$position = array_search($votes,$this->voteArray);
							//echo $position.'iq';
							//echo (($position+1)/$this->totalVotes ).'sd<br/>ps='.$position.'tot='.$this->totalVotes;
  							$iQ = (($position+1)/$this->totalVotes )* 100 ;
							//exit;
							break;
            
        
        }
		if($iQ > 0) {
			$iQ = round( $iQ );
			$iQ .= '%';
		}
        return $iQ;
	}
    
	public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
}
?>